<template>
  <div>
      <input type="text" v-model="aaa">
      <!-- <button @click="getsou(aaa)"> 搜索</button> -->
    <el-button type="primary" @click="getsou(aaa)">搜索</el-button>
    <el-button type="primary" @click="addFun">添 加</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="userName" label="用户名"> </el-table-column>
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登陆">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.lastLoginAt }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="role" label="角色">
        <template slot-scope="scope">
          <el-tag size="medium" type="danger" v-if="scope.row.role == 1"
            >超级管理员</el-tag
          >
          <el-tag size="medium" type="success" v-if="scope.row.role == 2"
            >管理员</el-tag
          >
          <el-tag size="medium" type="warning" v-if="scope.row.role == 3"
            >普通用户</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            title="您确定要删除吗？"
            @confirm="handleDelete(scope.$index, scope.row)"
          >
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
            <!-- @click="handleDelete(scope.$index, scope.row)" -->
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="ruleForm.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="ruleForm.rows"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="addForm">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="userName"
        >
          <el-input v-model="form.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="passWord"
        >
          <el-input v-model="form.passWord" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="超级管理员" :value="1"></el-option>
            <el-option label="管理员" :value="2"></el-option>
            <el-option label="普通用户" :value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑" :visible.sync="editFormVisible">
      <el-form :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="userName"
        >
          <el-input v-model="editForm.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="passWord"
        >
          <el-input v-model="editForm.passWord" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth" prop="role">
          <el-select v-model="editForm.role" placeholder="请选择角色">
            <el-option label="超级管理员" :value="1"></el-option>
            <el-option label="管理员" :value="2"></el-option>
            <el-option label="普通用户" :value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="EditFun">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
        aaa:"",
      formLabelWidth: "120px",
      dialogFormVisible: false,
      form: {
        name: "",
        userName: "",
        passWord: "",
        role: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        passWord: [{ required: true, message: "请输入密码", trigger: "blur" }],
        role: [{ required: true, message: "请选中角色", trigger: "change" }],
      },
      editFormVisible: false,
      editForm: {
        name: "",
        userName: "",
        passWord: "",
        role: "",
      },

      ruleForm: {
        page: 1,
        rows: 10,
        userName:""
      },
      total: 0,
      tableData: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // this.$axios.get('/wish/123')

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.ruleForm.rows = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.ruleForm.page = val;
      this.getList();
    },
    /**
     * 新增用户
     */
    handleAdd() {
      this.$refs["addForm"].validate(async (valid) => {
        if (valid) {
          let res = await this.$axios.post("/admin", this.form);
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: res.data.msg,
            });
            this.dialogFormVisible = false;
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    /**
     * 编辑用户
     */
    EditFun() {
      console.log(this.editForm);

      this.$refs["editForm"].validate(async (valid) => {
        if (valid) {
          let res = await this.$axios.put("/admin", this.editForm);
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: res.data.msg,
            });
            this.editFormVisible = false;
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addFun() {
      this.dialogFormVisible = true;
    },
    getsou(aaa){
        this.ruleForm.userName = aaa;
        this.getList();
    },
    handleEdit(index, row) {
      console.log(index, row);
      //   this.editForm = Object.assign({},row);
      this.editForm = structuredClone(row);
      console.log(this.editForm);
      this.editFormVisible = true;
    },
    async handleDelete(index, row) {
      console.log(index, row);
      let res = await this.$axios.delete("/admin", {
        data: {
          id: row.id,
        },
      });
      if (res.data.code == 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        this.getList();
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    async getList() {
      let res = await this.$axios.get("/admin", {
        params: this.ruleForm,
      });
      console.log(res);
      if (res.data.code == 200) {
        this.tableData = res.data.data.list;
        this.total = res.data.data.count;
      }
    },
  },
};
</script>

<style>
</style>